package vn.orderonline.com.core.business.toorder;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.orderonline.com.core.business.dto.OrderBillDto;
import vn.orderonline.com.core.business.dto.ToOrderDto;
import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.ResideDao;
import vn.orderonline.core.dao.RestaurantDao;
import vn.orderonline.core.dao.entity.DrinkBasket;
import vn.orderonline.core.dao.entity.DrinkBasketId;
import vn.orderonline.core.dao.entity.FoodBasket;
import vn.orderonline.core.dao.entity.FoodBasketId;
import vn.orderonline.core.dao.entity.Bill;
import vn.orderonline.core.dao.entity.TemporaryDrinkBasket;
import vn.orderonline.core.dao.entity.TemporaryFoodBasket;
import vn.orderonline.core.dao.entity.ToOrder;
import vn.orderonline.core.dao.temporaryorder.TemporaryDrinkBasketDao;
import vn.orderonline.core.dao.temporaryorder.TemporaryFoodBasketDao;
import vn.orderonline.core.dao.toorder.DrinkBasketDao;
import vn.orderonline.core.dao.toorder.FoodBasketDao;
import vn.orderonline.core.dao.toorder.BillDao;
import vn.orderonline.core.dao.toorder.ToOrderDao;
import vn.orderonline.core.mail.ISendMail;

@Service
@Transactional
public class ToOrderImpl implements IToOrder {

	@Autowired
	private ToOrderDao toOrderDao;
	@Autowired
	private TemporaryFoodBasketDao temporaryFoodBasketDao;
	@Autowired
	private TemporaryDrinkBasketDao temporaryDrinkBasketDao;
	@Autowired
	private FoodBasketDao basketFoodDao;
	@Autowired
	private DrinkBasketDao basketDrinkDao;
	@Autowired
	private BillDao billDao;
	@Autowired
	private ISendMail sendMail;
	@Autowired
	private ResideDao resideDao;
	@Autowired
	private RestaurantDao restaurantDao;

	@Override
	public String toOrder(ToOrderDto dto) {
		ToOrder toOrder = new ToOrder();
		toOrder.setOrderCode(createOrderCode());
		toOrder.setPersonalOrCompany(true);
		toOrder.setUsername(dto.getUsername());
		toOrder.setCityName(dto.getCityName());
		toOrder.setDistrictName(dto.getDistrictName());
		toOrder.setWardName(dto.getWardName());
		toOrder.setEmail(dto.getEmail());
		toOrder.setPhoneNumber(dto.getPhoneNumber());
		toOrder.setReside(dto.getReside().toString());
		toOrder.setCreateTime(new Timestamp(DateUtils.now()));
		toOrder.setRestaurantId(dto.getRestaurantId());
		toOrder.setMemberId(dto.getMemberId());
		toOrderDao.save(toOrder);

		Bill bill = new Bill();
		bill.setOrderId(toOrder.getOrderId());
		bill.setTotalPrice(dto.getCount());
		bill.setDeliveryPrice(dto.getDeliveryPrice());
		bill.setPaymentMethod("1");
		bill.setNote(dto.getNote());
		billDao.save(bill);

		List<TemporaryFoodBasket> listFood = temporaryFoodBasketDao
				.getListFoodByRestarantId(dto.getRestaurantId());
		if (listFood != null) {
			FoodBasket basketFood = null;
			for (TemporaryFoodBasket tem : listFood) {
				basketFood = new FoodBasket();
				basketFood.setId(new FoodBasketId(toOrder.getOrderId(), tem
						.getId().getFoodId()));
				basketFood.setAmount(tem.getAmount());
				basketFood.setPrice(tem.getPrice());
				basketFood.setFoodName(tem.getFoodName());
				basketFoodDao.save(basketFood);
			}
		}

		List<TemporaryDrinkBasket> listDrink = temporaryDrinkBasketDao
				.getListDrinkByRestaurantId(dto.getRestaurantId());
		if (listDrink != null) {
			DrinkBasket basketDrink = null;
			for (TemporaryDrinkBasket tem : listDrink) {
				basketDrink = new DrinkBasket();
				basketDrink.setId(new DrinkBasketId(toOrder.getOrderId(), tem
						.getId().getDrinkId()));
				basketDrink.setDrinkName(tem.getDrinkName());
				basketDrink.setPrice(tem.getPrice());
				basketDrink.setAmount(tem.getAmount());
				basketDrinkDao.save(basketDrink);
			}

		}

		// Delete temporary table
		for (TemporaryFoodBasket tem : listFood) {
			temporaryFoodBasketDao.delete(tem);
		}
		for (TemporaryDrinkBasket tem : listDrink) {
			temporaryDrinkBasketDao.delete(tem);
		}

		// sendMail.mailToOrder("dinhltuit@gmail.com", "ada");

		return toOrder.getOrderCode();
	}

	private String createOrderCode() {
		Calendar calendar = Calendar.getInstance();
		String s = "KH";
		s += calendar.get(Calendar.YEAR);
		Integer month = calendar.get(Calendar.MONTH) + 1;
		if (month < 10) {
			s += "0" + month;
		} else {
			s += month;
		}
		Integer day = calendar.get(Calendar.DAY_OF_MONTH);
		if (day < 10) {
			s += "0" + day;
		} else {
			s += day;
		}
		Integer hour = calendar.get(Calendar.HOUR_OF_DAY);
		if (hour < 10) {
			s += "0" + hour;
		} else {
			s += hour;
		}
		Integer minute = calendar.get(Calendar.MINUTE);
		if (minute < 10) {
			s += "0" + minute;
		} else {
			s += minute;
		}
		Integer second = calendar.get(Calendar.SECOND);
		if (second < 10) {
			s += "0" + second;
		} else {
			s += second;
		}
		return s;
	}

	@Override
	public List<OrderBillDto> getAllBillOrder() {
		List<Object[]> list = toOrderDao.getAllOrderBill();
		if (!list.isEmpty()) {
			List<OrderBillDto> listBill = new ArrayList<>();
			OrderBillDto dto = null;
			for (Object[] obj : list) {
				dto = new OrderBillDto();
				dto.setOrderCode((String) obj[0]);
				dto.setUsername((String) obj[1]);
				dto.setEmail((String) obj[2]);
				dto.setWardName((String) obj[3]);
				dto.setPhoneNumber((String) obj[4]);
				dto.setRestaurantName((String) obj[5]);
				dto.setTotalPrice((Float) obj[6]);
				dto.setCreateTime((Timestamp) obj[7]);
				listBill.add(dto);
			}
			return listBill;
		}
		return null;
	}

	@Override
	public OrderBillDto getOrder(String orderCode) {
		OrderBillDto dto = null;
		if (!StringUtils.isNullOrEmpty(orderCode)) {
			List<ToOrder> list = toOrderDao.findByProp("orderCode", orderCode);
			if (!list.isEmpty()) {
				ToOrder toOrder = list.get(0);
				if (toOrder != null) {
					dto = new OrderBillDto();
					dto.setOrderId(toOrder.getOrderId());
					dto.setOrderCode(toOrder.getOrderCode());
					dto.setPersonalOrCompany(toOrder.getPersonalOrCompany());
					dto.setUsername(toOrder.getUsername());
					dto.setCityName(toOrder.getCityName());
					dto.setDistrictName(toOrder.getDistrictName());
					dto.setWardName(toOrder.getWardName());
					dto.setEmail(toOrder.getEmail());
					dto.setPhoneNumber(toOrder.getPhoneNumber());
					dto.setResideName(resideDao.getResideNameByResideId(Integer
							.valueOf(toOrder.getReside())));
					dto.setCreateTime(toOrder.getCreateTime());
					dto.setRestaurantName(restaurantDao
							.getRestaurantNameByRestaurantId(toOrder
									.getRestaurantId()));

					Bill bi = billDao.getBillByOrderId(dto.getOrderId());
					if (bi != null) {
						dto.setBillId(bi.getBillId());
						dto.setTotalPrice(bi.getTotalPrice());
						dto.setDeliveryPrice(bi.getDeliveryPrice());
						dto.setPaymentMethod(Integer.valueOf(bi
								.getPaymentMethod()));
						dto.setNote(bi.getNote());
					}

				}
			}
		}
		return dto;
	}

	@Override
	public Integer getOrderIdByOrderCode(String orderCode) {
		if (!StringUtils.isNullOrEmpty(orderCode)) {
			return toOrderDao.getOrderIdByOrderCode(orderCode);
		}
		return null;
	}

	@Override
	public List<OrderBillDto> search(ToOrderDto toOrderDto) {
		List<Object[]> list = toOrderDao.search(toOrderDto.getOrderCode(),
				toOrderDto.getUsername(), toOrderDto.getEmail(),
				toOrderDto.getWardName(), toOrderDto.getPhoneNumber(),
				toOrderDto.getRestaurantName());
		if (!list.isEmpty()) {
			List<OrderBillDto> listBill = new ArrayList<>();
			OrderBillDto dto = null;
			for (Object[] obj : list) {
				dto = new OrderBillDto();
				dto.setOrderCode((String) obj[0]);
				dto.setUsername((String) obj[1]);
				dto.setEmail((String) obj[2]);
				dto.setWardName((String) obj[3]);
				dto.setPhoneNumber((String) obj[4]);
				dto.setRestaurantName((String) obj[5]);
				dto.setTotalPrice((Float) obj[6]);
				dto.setCreateTime((Timestamp) obj[7]);
				listBill.add(dto);
			}
			return listBill;
		}
		return null;
	}

}
